package com.sense.kukoo.common.wiget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.sense.kukoo.common.R;

public class TopTabLayout extends TabLayout {
    private OnTabListener mOnTabListener;
    private String[] mTitles;
    private int color_text_active;
    private int color_text_primary;

    public interface OnTabListener {
        void onTabListener(int position);
    }

    public void setOnTabListener(OnTabListener onTabListener) {
        this.mOnTabListener = onTabListener;
    }

    public TopTabLayout(Context context) {
        super(context);
    }

    public TopTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TopTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * @Author Lionszhang
     * @Date 2021/4/14 9:05
     * @Instruction 初始化數據
     */
    public void init(String[] titles) {
        this.mTitles = titles;
        color_text_active = getResources().getColor(com.sense.kukoo.common_design.R.color.text_active);
        color_text_primary = getResources().getColor(com.sense.kukoo.common_design.R.color.text_primary);

        this.addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    TextView tab_layout_text = tab.getCustomView().findViewById(R.id.top_tab_layout_text);
                    //
                    tab_layout_text.setTextColor(color_text_active);
                    tab_layout_text.setBackground(getResources().getDrawable(com.sense.kukoo.common_design.R.drawable.design_bg_top_tab_selected));
                    if (mOnTabListener != null) {
                        mOnTabListener.onTabListener(tab.getPosition());
                    }
                }
            }

            @Override
            public void onTabUnselected(Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    TextView tab_layout_text = tab.getCustomView().findViewById(R.id.top_tab_layout_text);
                    //
                    tab_layout_text.setTextColor(color_text_primary);
                    tab_layout_text.setBackground(getResources().getDrawable(com.sense.kukoo.common_design.R.drawable.design_bg_top_tab_unselected));
                }
            }

            @Override
            public void onTabReselected(Tab tab) {
            }
        });
    }

    /**
     * @Author Lionszhang
     * @Date 2021/4/14 9:06
     * @Para tab 选项卡tab,position 位置,titles 选项卡标题
     * @Instruction 设置选项卡标题
     */
    public void setTab(Tab tab, int position) {
        tab.setCustomView(R.layout.common_top_tab_view);
        View customView = tab.getCustomView();
        if (customView != null) {
            TextView top_tab_layout_text = customView.findViewById(R.id.top_tab_layout_text);
            //
            top_tab_layout_text.setText(mTitles[position]);
            top_tab_layout_text.setTextColor(color_text_primary);
            top_tab_layout_text.setBackground(getResources().getDrawable(com.sense.kukoo.common_design.R.drawable.design_bg_top_tab_unselected));

            /*if(position == 0){
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) customView.getLayoutParams();
                layoutParams.setMarginStart(ApplicationContext.dp2px(8));
            }else if(position ==( mTitles.length - 1)){
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) customView.getLayoutParams();
                layoutParams.setMarginEnd(ApplicationContext.dp2px(8));
            }else{
                *//*LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) customView.getLayoutParams();
                LoggerManager.e(TAG,"position == "+position);
                LoggerManager.e(TAG,"layoutParams.getMarginStart()="+layoutParams.getMarginStart());
                LoggerManager.e(TAG,"layoutParams.getMarginEnd()="+layoutParams.getMarginEnd());*//*
            }*/
        }
    }
}
